/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.UserBusinessDao;
import com.boxin.ims.modules.wechat.entity.UserBusiness;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 用户业务服务定义类Service
 * @author Jakemanse
 * @version 2013-09-27
 */
@Component
@Transactional(readOnly = true)
public class UserBusinessService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(UserBusinessService.class);
	
	@Autowired
	private UserBusinessDao userBusinessDao;
	
	public UserBusiness get(Long id) {
		return userBusinessDao.findOne(id);
	}
	
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-28  上午8:25:36
	 * @function <p> 获取该用户下所有微信服务列表 </p>
	 * @param userId
	 * @return
	 */
	public List<UserBusiness> findAllByUserId(Long userId){
		DetachedCriteria dc = userBusinessDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("delFlag", UserBusiness.DEL_FLAG_NORMAL));
		return userBusinessDao.find(dc);
	}
	
	public UserBusiness getByUserIdAndConfigId(Long userId,Long configId){
		DetachedCriteria dc = userBusinessDao.createDetachedCriteria();
		dc.createAlias("businessConfig", "businessConfig");
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("businessConfig.id", configId));
		
		List<UserBusiness> result = userBusinessDao.find(dc);
		return (result == null || result.isEmpty()) ? null : result.get(0);
	}
	
	public Page<UserBusiness> find(Page<UserBusiness> page, UserBusiness userBusiness) {
		DetachedCriteria dc = userBusinessDao.createDetachedCriteria();
		if (userBusiness.getUser()!=null && userBusiness.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", userBusiness.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(userBusiness.getName())){
			dc.add(Restrictions.like("name", "%"+userBusiness.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(userBusiness.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+userBusiness.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", UserBusiness.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return userBusinessDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(UserBusiness userBusiness) {
		userBusiness.setContent(StringEscapeUtils.unescapeHtml4(userBusiness.getContent()));
		userBusinessDao.save(userBusiness);
	}
	
	
	public int updateReplyType(String msgType,Long id){
		return userBusinessDao.updateReplyType(msgType, id);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		userBusinessDao.deleteById(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-24  上午11:39:02
	 * @function <p> 所有的都失效  </p>
	 * @param userId
	 */
	public void invalidAllByUserId(Long userId){
		userBusinessDao.invalidAllByUserId(userId);
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-10-24  上午11:36:27
	 * @function <p> 失效 </p>
	 * @param userId
	 * @param configId
	 */
	public void invalidByUserIdAndConfigId(Long userId,Long configId){
		userBusinessDao.invalidByUserIdAndConfigId(userId,configId);
	}
	/**
	 * @author Jakemanse
	 * @time 2013-10-24  上午11:36:49
	 * @function <p> 生效 </p>
	 * @param userId
	 * @param configId
	 */
	public void validByUserIdAndConfigId(Long userId,Long configId){
		userBusinessDao.validByUserIdAndConfigId(userId,configId);
	}
	
}
